package com.tuiyun.jxgl.action;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.transaction.annotation.Transactional;

import com.tuiyun.jxgl.bean.Driver;
import com.tuiyun.jxgl.bean.Student;
import com.tuiyun.jxgl.bean.Stuselectdriver;
import com.tuiyun.jxgl.dao.StudentHome;
import com.tuiyun.jxgl.dao.StuselectdriverHome;

public class ListStudentAction {
	
	
	private int state = -1;
	
	private StudentHome sh;
	private StuselectdriverHome ssh;
	
	public StudentHome getSh() {
		return sh;
	}
	public void setSh(StudentHome sh) {
		this.sh = sh;
	}
	private String msg;
	private int msg_type;
	
	private String q;
	
	private int total;
	private int page_size;
	private int page_index;
	private int page_count;
	private List<Student> lds;
	private int driverId;
	@Transactional
	public String execute(){
		Subject currentUser = SecurityUtils.getSubject();
		if(currentUser.isAuthenticated()){
			Object o = currentUser.getPrincipal();
			if(o instanceof Student){
				//o should be admin
				Student s = (Student)o;
				if(s.getName().equalsIgnoreCase("admin")){
					//get all students
					page_size = 10;
					if(q != null && q.length() >= 0){
						total = sh.findCountLikeName(q,state);						
						setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
						if(page_index <= 0)
							page_index = 1;
						if(page_index >= page_count)
							page_index = page_count;
						lds = sh.findByRangeLikeName(page_size, page_index,q,state);
						for(Student stu:lds){
							Driver dr = ssh.findDrierByStudent(stu.getId());
							if(dr != null){
								stu.setDriverId(dr.getId());
								stu.setDriverName(dr.getRealName());
							}else{
								stu.setDriverId(-1);
								stu.setDriverName("未指定");
							}
						}
					}else{
						total = sh.findCount(state);
						setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
						if(page_index <= 0)
							page_index = 1;
						if(page_index >= page_count)
							page_index = page_count;
						lds = sh.findByRange(page_size, page_index,state);
						for(Student stu:lds){
							Driver dr = ssh.findDrierByStudent(stu.getId());
							if(dr != null){
								stu.setDriverId(dr.getId());
								stu.setDriverName(dr.getRealName());
							}else{
								stu.setDriverId(-1);
								stu.setDriverName("未指定");
							}
						}
					}
					return "success";
				}
			}else{
				//o is a driver. get only my students
				Driver d = (Driver)o;
				page_size = 10;
				if(q != null && q.length() >= 0){
					total = sh.findCountLikeNameByDriver(q,d.getId(),state);
					setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
					if(page_index <= 0)
						page_index = 1;
					if(page_index >= page_count)
						page_index = page_count;
					lds = sh.findByRangeLikeNameAndDriver(page_size, page_index,q,d.getId(),state);
				}else{
					total = sh.findCountByDriver(d.getId(),state);
					setPage_count((int) Math.floor(total*1.0/page_size + 0.5));
					if(page_index <= 0)
						page_index = 1;
					if(page_index >= page_count)
						page_index = page_count;
					lds = sh.findByRangeAndDriver(page_size, page_index,d.getId(),state);
				}
				return "success";
			}			
		}
		return "error";
		
	}
	public int getTotal() {
		return total;
	}
	public void setTotal(int total) {
		this.total = total;
	}
	public int getPage_size() {
		return page_size;
	}
	public void setPage_size(int page_size) {
		this.page_size = page_size;
	}
	public int getPage_index() {
		return page_index;
	}
	public void setPage_index(int page_index) {
		this.page_index = page_index;
	}	
	public List<Student> getLds() {
		return lds;
	}
	public void setLds(List<Student> lds) {
		this.lds = lds;
	}
	public int getPage_count() {
		return page_count;
	}
	public void setPage_count(int page_count) {
		this.page_count = page_count;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public int getMsg_type() {
		return msg_type;
	}
	public void setMsg_type(int msg_type) {
		this.msg_type = msg_type;
	}	
	public int getDriverId() {
		return driverId;
	}
	public void setDriverId(int driverId) {
		this.driverId = driverId;
	}
	public String getQ() {
		return q;
	}
	public void setQ(String q) {
		this.q = q;
	}
	public int getState() {
		return state;
	}
	public void setState(int state) {
		this.state = state;
	}
	public StuselectdriverHome getSsh() {
		return ssh;
	}
	public void setSsh(StuselectdriverHome ssh) {
		this.ssh = ssh;
	}	
	
}
